// subpkg/money/money.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:'',
    count:'',
    accountList:[],
    show:false,
    date1:'',
    date:'',
    currentDate: new Date().getTime(),
    minDate: new Date(2023,10,1).getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  bindCount(e){
    this.setData({
      count:e.detail.value
    })
  },

  bindDetail(e){
    this.setData({
      detail:e.detail.value
    })
  },

  showPopup(){
    this.setData({
      show:true
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  onConfirm(){
    this.setData({
      date:this.data.date1,
      show:false
    })
  },
  onCancel(){
    this.setData({
      show:false
    })
  },
  onInput(event) {
    var date = new Date(event.detail)
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`
    console.log(currentDate)
    
    this.setData({
      date1: currentDate,
    });
  },
  add(){
    if(this.data.count===''||this.data.detail===''){
      Toast('请先完善信息')
      return
    }
    wx.request({
      url: 'https://zhixiao.site:8080/client/bill/add',
      method:'POST',
      data:{
        amount: this.data.count,
        userId: wx.getStorageSync('elderId'),
        detail: this.data.detail,
        spendTime:this.data.date
      },
      header:{
        "Content-Type": "application/json",
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        console.log(res)
        Toast('添加成功')
      },
      complete:(res)=>{
        this.query()
        this.setData({
          detail:'',
          count:''
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth()+1 <10 ? `0${date.getMonth()+1}` : date.getMonth()+1
    var day = date.getDate()<10?`0${date.getDate()}` : date.getDate()
    var currentDate = `${year}-${month}-${day}`
    console.log(currentDate)
    this.setData({
      date: currentDate,
    });
    this.query()
  },
  query(){
    wx.request({
      url: 'https://zhixiao.site:8080/client/bill/query',
      data:{
        id:wx.getStorageSync('elderId')
      },
      header:{
        'Cookie':wx.getStorageSync('JSESSIONID')
      },
      success:(res)=>{
        var accountList = res.data.data
        console.log(res)
        accountList.reverse()
        this.setData({
          accountList:accountList
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})